<template>
  <div :style="{width: width, height: height}" ref="chart"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'u-echart',
  props: {
    option: {
      type: Object,
      required: true,
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '100%',
    }
  },
  data() {
    return {
      chartInstance: null
    }
  },
  watch: {
    option: {
      handler(newOption) {
        if (this.chartInstance) {
          this.chartInstance.setOption(newOption);
        }
      },
      deep: true
    }
  },
  mounted() {
    this.chartInstance = echarts.init(this.$refs.chart);
    this.chartInstance.setOption(this.option);
  },
  beforeDestroy() {
    if (this.chartInstance) {
      this.chartInstance.dispose();
    }
  }
}
</script>
